Ext.define('BenBen.common.GridField', {
	extend : 'Ext.form.TriggerField',
	xtype : 'gridfield',
	hiddenField: null,
	hiddenId:'',
	displayField:'' ,
	hiddenValue:'',
	gridSingleSelect : true,
	valueIndex : 1,
	initComponent : function() {
		if (this.basePanel) {
			var name = this.prefixName +"." + this.basePanel.gridPanel.columns[this.valueIndex].dataIndex;
			var hiddenName = this.prefixName +".id";
			Ext.apply(this, {
				name : name,
				hiddenName : hiddenName
			});
			
		}
		this.callParent(arguments);
	},
	initWindow : function(){
		if (this.basePanel) {
			this.grid = this.basePanel.gridPanel;
			this.store = this.grid.store;
			if (!!this.gridHeight) {
				this.grid.setHeight(this.gridHeight);
			}

			if (this.gridSingleSelect) {
				this.grid.addListener('itemdblclick', function(){
					this.sureBtnClick();
				}, this);
			}
		}
		
		var panel = new Ext.Panel({
			border : false,
			plain : true,
			width : 500,
			autoHeight : true,
			buttonAlign : 'center',
			buttons : [{
				text : '确定',
				iconCls : 'sure',
				scope : this,
				handler : this.sureBtnClick
			}, {
				text : '取消',
				iconCls : 'cancel',
				scope : this,
				handler : function() {
					this.baseWindow.hide();
				}
			}],
			defaults : {
				columnWidth : 1,
				border : false,
				layout : 'fit'
			},
			layout : 'column',
			autoScroll : true,
			items : [this.basePanel]
		});

		this.panel = panel;

		if (this.fieldPlugins !== undefined) {
			panel.insert(0, form);
		}

		var initHeight=this.gridHeight+200;
		
		this.baseWindow = new Ext.Window({
			layout : 'fit',
			width : this.gridWidth || 550,
			closeAction : 'hide',
			height : initHeight,
			resizable : true,
			shadow : true,
			maximizable : true,
			modal : true,
			closable : true,
			iconCls : 'window',
			title : '选择窗口',
			animCollapse : true,
			items : [panel]
		});

	},
	afterRender : function(ct, position){  
        if(this.hiddenName){  
        	 //在渲染时 创建隐藏域的DOM  
            this.hiddenField =  Ext.create('Ext.form.field.Hidden',{
            		isSearch : true, 
            		name: this.hiddenName
            }); 
            this.ownerCt.add(this.hiddenField);
            this.ownerCt.doLayout();
           
        }  
    }, 
	onTriggerClick : function(){
		this.initWindow();
		this.baseWindow.show();
	},
	sureBtnClick : function(){
		var record = this.grid.getSelectionModel().getSelection()[0];
		var displayValue=record.get(this.grid.columns[this.valueIndex].dataIndex);
		var value=record.get('id');
		this.setValue(displayValue);
        this.setHiddenValue(value);
		this.baseWindow.hide();
	},
	setValue:function(value){
		this.value=value;
		if(this.emptyText && this.el && value!==undefined && value!==null && value!==''){ 
			this.el.removeClass(this.emptyClass); 
		}
		BenBen.common.GridField.superclass.setValue.call(this, value);
		this.applyEmptyText(); 
		this.autoSize(); 
	},
	setHiddenValue:function(hiddenValue){
		this.hiddenValue=hiddenValue;
		this.hiddenField.setValue(hiddenValue);
	},
	getHiddenValue:function(){
		return this.hiddenValue;
	}
});
